package com.sgai.zero_robot.pop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.sgai.zero_robot.R;
import com.sgai.zero_robot.pojo.SettingBean;

import java.util.List;

public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.SettingHolder> {
    private final Context context;
    private final LayoutInflater mLayoutInflater;
    private final List<SettingBean> list;
    private int selectIndex = -1;
    private SettingCallback settingCallback;
    public SettingAdapter(Context context, List<SettingBean> list, SettingCallback settingCallback){
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.list=list;
        this.settingCallback=settingCallback;
    }
    @NonNull
    @Override
    public SettingHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SettingHolder(mLayoutInflater.inflate(R.layout.item_setting,parent,false));
    }
    public void setSelectIndex(int index){
        if (index == selectIndex){
            return;
        }
        selectIndex = index;
        notifyDataSetChanged();
    }
    @Override
    public void onBindViewHolder(@NonNull SettingHolder holder, @SuppressLint("RecyclerView") int position) {
        if (position == selectIndex){
            holder.ivIcon.setImageResource(list.get(position).resPre);
        }else{
            holder.ivIcon.setImageResource(list.get(position).resNor);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectIndex == position){
                    return;
                }
                int temo = selectIndex;
                selectIndex = position;
                notifyItemChanged(temo);
                notifyItemChanged(selectIndex);
                if (settingCallback!=null){
                    settingCallback.onSelect(position,list.get(position).title);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public static class SettingHolder extends RecyclerView.ViewHolder{

        public final ImageView ivIcon;

        public SettingHolder(@NonNull View itemView) {
            super(itemView);
            ivIcon = itemView.findViewById(R.id.iv_icon);
        }
    }

    public int getSelectIndex() {
        return selectIndex;
    }
}
